home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Interfaces / CIncludes / stdio.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-05  |  7.5 KB  |  296 lines  |  [TEXT/MPS ]

  1. /************************************************************
  2.  
  3.     stdio.h
  4.     Standard input and output.
  5.  
  6.     Copyright Apple Computer,Inc.  1995
  7.     All rights reserved
  8.  
  9. ************************************************************/
  10.  
  11. /* Conditional Macros:
  12.  *  UsingStaticLibs - for CFM-68K:  Insures that #pragma import is never used.
  13.  *  <none>          - for CFM-68K:  Insures that all functions and data items are
  14.  *                                  marked as library imports
  15.  */
  16.  
  17.  
  18. #ifndef __STDIO__
  19. #define __STDIO__
  20.  
  21. /*
  22.  * Include common declarations 
  23.  */
  24.  
  25. #include <NullDef.h>
  26. #include <SizeTDef.h>
  27. #include <SeekDefs.h>
  28. #include <VaListTDef.h>
  29.  
  30.  
  31. /*
  32.  *  The basic data structure for a stream is the FILE type.
  33.  */
  34.  
  35. #ifdef powerc
  36. #pragma options align=power
  37. #endif
  38. struct FILE {
  39.     int             _cnt;
  40.     unsigned char   *_ptr;
  41.     unsigned char   *_base;
  42.     unsigned char   *_end;
  43.     unsigned short  _size;
  44.     unsigned short  _flag;
  45.     unsigned short  _file;
  46. };
  47. #ifdef powerc
  48. #pragma options align=reset
  49. #endif
  50.  
  51. typedef struct FILE FILE;
  52.  
  53.  
  54. /*
  55.  *  The type to uniquely specify the position in a file, returned by fgetpos().
  56.  */
  57.  
  58. typedef long fpos_t;
  59.  
  60.  
  61. /*
  62.  *  Values for various bits of a FILE's _flag field.
  63.  *  The values _IOFBF, _IOLBF, and _IONBF can be used as the
  64.  *  third argument to setvbuf();
  65.  */
  66.  
  67. #define _IOFBF      0x00    /* File is fully buffered       */
  68. #define _IOREAD     0x01    /* File is open for Reading     */
  69. #define _IOWRT      0x02    /* File is open for Wrinting    */
  70. #define _IONBF      0x04    /* File I/O is unbuffered       */
  71. #define _IOMYBUF    0x08    /* Buffer allocated by stdio    */
  72. #define _IOEOF      0x10    /* End of file reached          */
  73. #define _IOERR      0x20    /* I/O error has occurred       */
  74. #define _IOLBF      0x40    /* File is line buffered        */
  75. #define _IORW       0x80    /* File is open for Read/Write  */
  76. #define _IOSYNC    0x100    /* Flush output on Read         */
  77. #define _IOBINARY  0x200    /* For backward compatibility   */
  78. #define _IOBACK   0x4000    /* For backward compatibility   */
  79.  
  80.  
  81. /*
  82.  *  The default buffer sizes for a fully buffered or line buffered file.
  83.  */
  84.  
  85. #define BUFSIZ    1024
  86. #define _LBFSIZ    254
  87.  
  88.  
  89. /*
  90.  *  The normal end-of-file indicator.
  91.  */
  92.  
  93. #define EOF       (-1)
  94.  
  95.  
  96. /*
  97.  *  FOPEN_MAX is the minimum number of files that a program is guaranteed to be able
  98.  *  to have open simultaneously (including the pre-opened stdin, stdout, and stderr).
  99.  *  The numbers are listed in Inside Macintosh, page IV-178, as:
  100.  *  64K ROM, 128K Macintosh     12 files
  101.  *  64K ROM, 512K Macintosh     40 files
  102.  *  128K ROM                    40 files per volume
  103.  *
  104.  *  FILENAME_MAX is the maximum length of a file name, including a trailing zero byte.
  105.  */
  106.  
  107. #define FOPEN_MAX      12
  108. #define FILENAME_MAX   32
  109.  
  110.  
  111. /*
  112.  *  L_tmpnam is the size of char array long enough to hold a temporary file name
  113.  *  generated by tmpnam(), including the trailing null byte.  The name is in the
  114.  *  form tmp.AAAXXXXXX, where AAA is a sequence of lower case letters ("aaa", "baa",
  115.  *  ... "zzz" on successive calls), and XXXXXX is a lower case letter followed by a sequence
  116.  *  of digits, all determined at runtime.
  117.  */
  118.  
  119. #define L_tmpnam       14
  120.  
  121.  
  122. /*
  123.  *  TMP_MAX is the number of distinct file names that tmpnam() can generate.
  124.  */
  125.  
  126. #define TMP_MAX     17576
  127.  
  128.  
  129. /*
  130.  *  The standard predefined streams: error, input, and output
  131.  */
  132.  
  133. #define stdin        (&_iob[0])
  134. #define stdout       (&_iob[1])
  135. #define stderr       (&_iob[2])
  136.  
  137.  
  138. #ifdef __cplusplus
  139. extern "C" {
  140. #endif
  141.  
  142. #if defined (__CFM68K__) && !defined (UsingStaticLibs)
  143.     #pragma import on
  144. #endif
  145.  
  146. /*
  147.  * Operations on Files
  148.  */
  149.  
  150. extern int remove (const char *filename);
  151. extern int rename (const char *oldname, const char *newname);
  152. extern FILE *tmpfile (void);
  153. extern char *tmpnam (char *s);
  154.  
  155.  
  156. /*
  157.  * File Access Functions
  158.  */
  159.  
  160. extern int fclose (FILE *stream);
  161. extern int fflush (FILE *stream);
  162. extern FILE *fopen (const char *filename, const char *mode);
  163. extern FILE *freopen (const char *filename, const char *mode, FILE *stream);
  164. extern void setbuf (FILE *stream, char *buf);
  165. extern int setvbuf (FILE *stream, char *buf, int mode, size_t size);
  166.  
  167.  
  168. /*
  169.  * Formatted Input/Output Functions
  170.  */
  171.  
  172. extern int fprintf (FILE *stream, const char *format, ...);
  173. extern int fscanf (FILE *stream, const char *format, ...);
  174. extern int printf (const char *format, ...);
  175. extern int scanf (const char *format, ...);
  176. extern int sprintf (char *s, const char *format, ...);
  177. extern int sscanf (const char *s, const char *format, ...);
  178. extern int vfprintf (FILE *stream, const char *format, va_list arg);
  179. extern int vprintf (const char *format, va_list arg);
  180. extern int vsprintf (char *s, const char *format, va_list arg);
  181.  
  182.  
  183. /*
  184.  * Character Input/Output Functions
  185.  */
  186.  
  187. extern int fgetc (FILE *stream);
  188. extern char *fgets (char *s, int n, FILE *stream);
  189. extern int fputc (int c, FILE *stream);
  190. extern int fputs (const char *s, FILE *stream);
  191. extern int getc (FILE *stream);
  192. extern int getchar (void);
  193. extern char *gets (char *s);
  194. extern int putc (int c, FILE *stream);
  195. extern int putchar (int c);
  196. extern int puts (const char *s);
  197. extern int ungetc (int c, FILE *stream);
  198.  
  199. /*
  200.  * WARNING!!
  201.  *
  202.  * These macros evaluate their arguments more than once.
  203.  * Be sure that evaluation of the "s" argument has no side effects.
  204.  *
  205.  * For example, using "getc(mychar++)" would cause "mychar" to be
  206.  * incremented twice.
  207.  *
  208.  * To avoid this, either assign "mychar" to a temporary, or put the
  209.  * function name in paranthesis, so that the macro is not envoked:
  210.  *    "(getc)(mychar++)"
  211.  *
  212.  */
  213.  
  214. #define getc(s) (--(s)->_cnt >= 0 ? (int) *(s)->_ptr++ : _filbuf(s))
  215. #define getchar() (getc(stdin))
  216. #define putc(c, s)  (--(s)->_cnt >= 0 ?  \
  217.                         ((int) (*(s)->_ptr++ = (unsigned char) (c))) : \
  218.                         _flsbuf((unsigned char) (c), (s)))
  219. #define putchar(c) (putc((c), stdout))
  220.  
  221.  
  222. /*
  223.  * Direct Input/Output Functions
  224.  */
  225.  
  226. extern size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream);
  227. extern size_t fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream);
  228.  
  229.  
  230. /*
  231.  * File Positioning Functions
  232.  */
  233.  
  234. extern int fgetpos (FILE *stream, fpos_t *pos);
  235. extern int fseek (FILE *stream, long int offset, int whence);
  236. extern int fsetpos (FILE *stream, const fpos_t *pos);
  237. extern long int ftell (FILE *stream);
  238. extern void rewind (FILE *stream);
  239.  
  240.  
  241. /*
  242.  * Error Handling Functions
  243.  */
  244.  
  245. extern void clearerr (FILE *stream);
  246. extern int feof (FILE *stream);
  247. extern int ferror (FILE *stream);
  248. extern void perror (const char *s);
  249.  
  250. #define clearerr(s) ((void)((s)->_flag &= ~(_IOERR | _IOEOF)))
  251. #define feof(s) ((s)->_flag & _IOEOF)
  252. #define ferror(s) ((s)->_flag & _IOERR)
  253.  
  254.  
  255. /*
  256.  * Internal structures exposed by previous macro definitions.
  257.  */
  258.  
  259. extern FILE _iob[];    /* Array of FILE control blocks. */
  260. #define _NFILE 40      /* Size of _iob. */
  261.  
  262. extern int _filbuf(FILE *);
  263. extern int _flsbuf(unsigned char, FILE *);
  264.  
  265.  
  266. /*
  267.  *  Non-ANSI extensions
  268.  *
  269.  * The prefered mechanism for enabling these is by defining __useAppleExts__.  
  270.  * In the absence of this symbol, the __STDC__ symbol is used to enable or
  271.  * disable these extentions.
  272.  */
  273.  
  274. #if defined (__useAppleExts__) || \
  275.      (defined (applec) && ! defined (__STDC__)) || \
  276.      (defined (__PPCC__) && __STDC__ == 0)
  277.  
  278. #define fileno(p)  (p)->_file
  279.  
  280. extern FILE *fdopen(int fildes, const char *mode);
  281. extern void fsetfileinfo (char *filename, unsigned long newcreator, unsigned long newtype);
  282. extern int getw(FILE *stream);
  283. extern int putw(int w, FILE *stream);
  284.  
  285. #endif
  286.  
  287. #if defined (__CFM68K__) && !defined (UsingStaticLibs)
  288.     #pragma import off
  289. #endif
  290.  
  291. #ifdef __cplusplus
  292. }
  293. #endif
  294.  
  295. #endif  /* __STDIO__ */
  296.